/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package handler;

import dao.ComentarioDao;
import dao.PostDao;
import entidades.Comentario;
import entidades.Post;
import entidades.Usuario;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

/**
 *
 * @author PARTICULAR
 */
@Named(value = "comentarioHandler")
@SessionScoped
public class ComentarioHandler implements Serializable {
    private PostDao daoPost = new PostDao();
    private ComentarioDao daoComentario = new ComentarioDao();
    
    private  Comentario c;
    private Usuario u;
    private Post p;
    private List<Comentario> comentarios = daoComentario.getAll();
    private int idPost;
    public ComentarioHandler() throws IOException {
    try {
            u = (Usuario) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("usuario");
//            c.setId_usuario(u);
        } finally {
            FacesContext.getCurrentInstance().getExternalContext().redirect("LoginInvalido.xhtml");
        }
    }

    public Comentario getC() {
        return c;
    }

    public void setC(Comentario c) {
        this.c = c;
    }

    public Usuario getU() {
        return u;
    }

    public void setU(Usuario u) {
        this.u = u;
    }

    public Post getP() {
        return p;
    }

    public void setP(Post p) {
        this.p = p;
    }

    public List<Comentario> getComentarios() {
        return comentarios;
    }

    public void setComentarios(List<Comentario> comentarios) {
        this.comentarios = comentarios;
    }
    
    public void salvar(){
        daoComentario.inserir(c);
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("Home.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ComentarioHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }
    
    public String comentar(Post p){
        System.out.println("Funcionou----"+p.getCorpo());
        this.p=p;
//        System.out.println("usuario:"+p.getId_usuario().getNome());
//        FacesContext.getCurrentInstance().getExternalContext().redirect("CadastroComentario.xhtml");
        return "CadastroComentario.xhtml";
    }
    
    
    
}
